package ar.edu.itba.it.pdc.configuration.models.filters.destinyFilters;

import ar.edu.itba.it.pdc.configuration.models.Response;
import ar.edu.itba.it.pdc.monitoring.ProxyMonitoring;

public class DestinyResourseSizeFilter extends DestinyFilter{
	
	private int allowedSize;
	
	public DestinyResourseSizeFilter(int allowedSize) {
		this.allowedSize = allowedSize;
		setApplyOnRequest(false);
		setApplyOnResponse(true);
	}
	
	@Override
	public boolean applyFilterOnResponse(Response resp) {
		return applyFilter(resp.getSize());
	}
	
	public boolean applyFilter(int size) {
		//Note: resp.getsize should take in account if the content-length is specified or not!
		if (allowedSize >= size) {
			return true;
		}
		ProxyMonitoring.getInstance().addSizeBlocks();
		return false;
	}
	
	@Override
	public String getCriteria() {
		return "Allowed Size:";
	}
	
	@Override
	public String getContent() {
		return "" + allowedSize;
	}
}
